﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class pages_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection conn = DB.GetCon();////根据数据库中的记录数自动产生一个数作为入库单ID
            conn.Open();
            this.AutoID(conn);
            bind();
            this.Label1 .Text =DB.TYPE_SECTION;
        }
    }
    //根据数据库中的记录数自动产生一个数作为入库单ID
    public void AutoID(SqlConnection conn)
    {
        try
        {
            SqlCommand com = new SqlCommand("select count(*) from tb_Enterprise", conn);
            int Count = Convert.ToInt32(com.ExecuteScalar());
            this.txtID.Text = Convert.ToString(Count + 1);
            
            if (conn.State == ConnectionState.Open)
            {
                com.Dispose();
                conn.Close();
            }
        }
        catch(Exception e)
        {
            conn.Close ();
            conn.Dispose ();
        }
    }
    public void bind()
    {
        DataSet ds = DB.reDs("select * from tb_Enterprise");
        gvEnter.DataSource = ds;
        this.gvEnter.DataBind();
    }
    //使页面恢复初始状态
    public void clear()
    {
         this.txtName.Text="";
         this.txtAddress.Text="";
         this.txtPhone.Text="";
         this.txtLinkMan.Text="";
         this.txtBank.Text="";
         this.txtBankAccounts.Text="";
         this.txtNet.Text="";
         this.txtEmail.Text="";
         this.CheckBox1.Checked = true;
         this.CheckBox2.Checked = true;
         this.CheckBox3.Checked = true;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string CustomerID = this.txtID.Text.Trim();
        string Name = this.txtName.Text.Trim();
        string Address = this.txtAddress.Text.Trim();
        string Phone = this.txtPhone.Text.Trim();
        string LinkMan = this.txtLinkMan.Text.Trim();
        string Bank = this.txtBank.Text.Trim();
        string BankAccounts = this.txtBankAccounts.Text.Trim();
        string Net = this.txtNet.Text.Trim();
        string Email = this.txtEmail.Text.Trim();
        string Type1 = string.Empty;
        if (this.CheckBox1.Checked)//判断是否供货商
        {
            Type1 = "是";
        }
        else
        {
            Type1 = "否";
        }
        string Type2 = string.Empty;
        if (this.CheckBox2.Checked)//判断是否维修商
        {
            Type2 = "是";
        }
        else
        {
            Type2 = "否";
        }
        string Type3 = string.Empty;
        if (this.CheckBox3.Checked)//判断是否客户
        {
            Type3 = "是";
        }
        else
        {
            Type3 = "否";
        }
        try
        {  
            //向往来单位表中添加数据
            DB.ExSql("insert into tb_Enterprise values('" + CustomerID + "','" + Name + "','" + Address + "','" + Phone + "','" + LinkMan + "','" + Bank + "','" + BankAccounts + "','" + Net + "','" + Email + "','" + Type1 + "','" + Type2 + "','" + Type3 + "');");
            //使页面恢复初始状态
            this.clear();
            bind();
            SqlConnection conn = DB.GetCon();
            conn.Open();
            AutoID(conn);//根据数据库中的记录数自动产生一个数作为入库单ID
        }
        catch (SqlException oe)
        {
           Response.Write("<script>alert('"+oe.Message.ToString()+"')</script>");
        }
    }
    protected void lbtnDelete_Load(object sender, EventArgs e)
    {
        //在页面加载时，给ID属性为lbtnDelete的LinkButton控件加上onclick事件，弹出警告对话框
        ((LinkButton)sender).Attributes["onclick"] = "javascript:return confirm('你确认要删除选中项吗？')";
    }
    protected void lbtnDelete_Command(object sender, CommandEventArgs e)
    {
        //获取lbtnDelete的LinkButton对象
        LinkButton lbtn = (LinkButton)sender;
        GridViewRow row = (GridViewRow)lbtn.Parent.Parent;//引用lbtnDelete控件的父控件上一级控件
        //取得该控件的Text值
        string id = ((DataControlFieldCell)this.gvEnter.Rows[row.RowIndex].Cells[1]).Text.Trim();
        DB.ExSql("delete from tb_Enterprise where CustomerID='" + id + "'");
        gvEnter.Rows[row.RowIndex].Visible = false;//在不重新绑定GridView控件的条件下，将删除的行先隐藏
        bind();
        SqlConnection conn = DB.GetCon();
        conn.Open();
        AutoID(conn);
    }
}
